package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;


    public PageResult pageSearch(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageSearch(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> list = page.getResult();

        return new PageResult(total, list);
    }

    public SetmealVO getSetmealById(Integer id) {
        SetmealVO dishVO = setmealMapper.getSetmealById(id);
        List<SetmealDish> setmealDishes = setmealMapper.getSetmealDishById(id);
        dishVO.setSetmealDishes(setmealDishes);
        return dishVO;
    }

    public List<SetmealDish> getSetmealDishById(Integer id) {
        return setmealMapper.getSetmealDishById(id);
    }

    @Transactional
    public void insertSetmeal(SetmealDTO setmealDTO) throws Exception {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(StatusConstant.DISABLE);
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insertSetmeal(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setmealMapper.insertBatch(setmealDishes);
        }

    }


    public void insertBatch(List<SetmealDish> setmealDishes) {
        setmealMapper.insertBatch(setmealDishes);
    }

    public void updateSetmeal(Setmeal setmeal) {
        setmealMapper.updateSetmeal(setmeal);
    }


    public void deleteSetmeal(List<Long> ids) {
        Long Status = getSetmealStatusById(ids);
        if (Status > 0) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        setmealMapper.deleteSetmeal(ids);
    }


    public void statusSetmeal(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.updateSetmeal(setmeal);
    }

    @Transactional
    public void updateSetmealWithSetmealDishes(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.updateSetmeal(setmeal);
        Long setmealId = setmeal.getId();
        List<Long> ids = new ArrayList<>();
        ids.add(setmealId);
        setmealMapper.deletSetmealDishesById(ids);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        log.info("setmealDish的值是:{}", setmealDishes);
        insertBatch(setmealDishes);
    }


    public Long getSetmealStatusById(List<Long> ids) {
        return setmealMapper.getSetmealStatusById(ids);
    }

    public void deletSetmealDishesById(List<Long> ids){
        setmealMapper.deletSetmealDishesById(ids);
    }

    public List<Setmeal> getSetmealCategoryId(String categoryId){
        return setmealMapper.getSetmealCategoryId(categoryId);
    }



}
